﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using yaTDL.Core.MVVM;
using yaTDL.Extensions;

namespace yaTDL
{
	/// <summary>
	/// Interaction logic for LoginWindow.xaml
	/// </summary>
	public partial class LoginWindow : Window
	{
        LoginViewModel viewModel;


		public LoginWindow(LoginViewModel viewModel)
		{
            this.viewModel = viewModel;
            this.DataContext = this.viewModel;
            viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(viewModel_PropertyChanged);
            viewModel.Completed += new EventHandler(viewModel_Completed);

			this.InitializeComponent();


            this.KeyDown += new KeyEventHandler(LoginWindow_KeyDown);
            this.KeyUp += new KeyEventHandler(LoginWindow_KeyUp);

            passwordBox.Password = viewModel.Password.Unwrap();
            passwordBox.PasswordChanged += new RoutedEventHandler(passwordBox_PasswordChanged);

            //Center the window on top of the applications main window
            this.Top = Application.Current.MainWindow.Top + ((Application.Current.MainWindow.Height - this.Height) / 2);
            this.Left = Application.Current.MainWindow.Left + ((Application.Current.MainWindow.Width - this.Width) / 2);
            this.Owner = App.Current.MainWindow;
		}


        private bool enterDown = false;

        void LoginWindow_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                if (viewModel.CancelCommand.CanExecute(null))
                {
                    viewModel.CancelCommand.Execute(null);
                }
            }
            else if (e.Key == Key.Enter && enterDown)
            {
                if (viewModel.LoginCommand.CanExecute(null))
                {
                    viewModel.LoginCommand.Execute(null);
                }

                enterDown = false;
            }
        }

        void LoginWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                enterDown = true;
            }
        }


        void viewModel_Completed(object sender, EventArgs e)
        {
            this.Close();
        }




        #region "Password Binding" //manually sets the PasswordBox's content and updates the ViewModel, as Data Binding is not possible for PasswordBox

        void passwordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (IsInitialized)
            {
                viewModel.Password = passwordBox.SecurePassword;
            }
        }

        void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Password")
            {
                if (passwordBox.Password != viewModel.Password.Unwrap())
                {
                    passwordBox.Password = viewModel.Password.Unwrap();
                }
            }
        }

        #endregion

        
	}
}